package com.dgty.dg_common.widget.decoration;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;

/**
 * Title: GridItemDecoration
 * Description:适配 recycleView GridLayoutManager 列 左右贴边，中间居中
 * Created DateTime: 2023-02-14 9:33
 * Created by xxl.
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

        private Activity mContext;
        private int space;  //位移间距
        private int mColumnCount;  //列数
        public GridItemDecoration(int space, int columnCount) {
            this.space = space;
            this.mColumnCount=columnCount;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            int position=parent.getChildAdapterPosition(view) %mColumnCount;
            outRect.left=position*(space/(mColumnCount-1) -space/mColumnCount );

            if (parent.getChildAdapterPosition(view) >= mColumnCount) {
                outRect.top = SizeUtils.dp2px( space/(mColumnCount+1));
            } else {
                outRect.top = 0;
            }
        }

}
